OOP calling convention for database functions. DBMS abstraction implemented by means...
[lhc/web/wiklou.git] / includes / SpecialWatchlist.php
1 <?php
2 require_once( "SpecialRecentchanges.php" );
3 require_once( "WatchedItem.php" );
4
5 function wfSpecialWatchlist()
6 {
7 global $wgUser, $wgOut, $wgLang, $wgTitle, $wgMemc;
8 global $wgUseWatchlistCache, $wgWLCacheTimeout, $wgDBname;
9 global $days, $limit, $target; # From query string
10 $fname = "wfSpecialWatchlist";
11
12 $wgOut->setPagetitle( wfMsg( "watchlist" ) );
13 $sub = wfMsg( "watchlistsub", $wgUser->getName() );
14 $wgOut->setSubtitle( $sub );
15 $wgOut->setRobotpolicy( "noindex,nofollow" );
16
17 $specialTitle = Title::makeTitle( NS_SPECIAL, "Watchlist" );
18
19 $uid = $wgUser->getID();
20 if( $uid == 0 ) {
21 $wgOut->addHTML( wfMsg( "nowatchlist" ) );
22 return;
23 }
24
25 global $action,$remove,$id;
26 if(($action == "submit") && isset($remove) && is_array($id)) {
27 $wgOut->addHTML( wfMsg( "removingchecked" ) );
28 foreach($id as $one) {
29 $t = Title::newFromURL( $one );
30 if($t->getDBkey() != "") {
31 $wl = WatchedItem::fromUserTitle( $wgUser, $t );
32 if( $wl->removeWatch() === false ) {
33 $wgOut->addHTML( "<br />\n" . wfMsg( "couldntremove", htmlspecialchars($one) ) );
34 } else {
35 $wgOut->addHTML( " (" . htmlspecialchars($one) . ")" );
36 }
37 } else {
38 $wgOut->addHTML( "<br />\n" . wfMsg( "iteminvalidname", htmlspecialchars($one) ) );
39 }
40 }
41 $wgOut->addHTML( "done.\n<p>" );
42 }
43
44 if ( $wgUseWatchlistCache ) {
45 $memckey = "$wgDBname:watchlist:id:" . $wgUser->getId();
46 $cache_s = @$wgMemc->get( $memckey );
47 if( $cache_s ){
48 $wgOut->addHTML( wfMsg("wlsaved") );
49 $wgOut->addHTML( $cache_s );
50 return;
51 }
52 }
53
54 $sql = "SELECT COUNT(*) AS n FROM watchlist WHERE wl_user=$uid";
55 $res = wfQuery( $sql, DB_READ );
56 $s = wfFetchObject( $res );
57 $nitems = $s->n;
58 if($nitems == 0) {
59 $wgOut->addHTML( wfMsg( "nowatchlist" ) );
60 return;
61 }
62
63 if ( ! isset( $days ) ) {
64 $big = 1000;
65 if($nitems > $big) {
66 # Set default cutoff shorter
67 $days = (12.0 / 24.0); # 12 hours...
68 } else {
69 $days = 3; # longer cutoff for shortlisters
70 }
71 } else {
72 $days = floatval($days);
73 }
74
75 if ( $days <= 0 ) {
76 $docutoff = '';
77 $cutoff = false;
78 $npages = wfMsg( "all" );
79 } else {
80 $docutoff = "AND cur_timestamp > '" .
81 ( $cutoff = wfUnix2Timestamp( time() - intval( $days * 86400 ) ) )
82 . "'";
83 $sql = "SELECT COUNT(*) AS n FROM cur WHERE cur_timestamp>'$cutoff'";
84 $res = wfQuery( $sql, DB_READ );
85 $s = wfFetchObject( $res );
86 $npages = $s->n;
87
88 }
89
90 if(isset($_REQUEST['magic'])) {
91 $wgOut->addHTML( wfMsg( "watchlistcontains", $wgLang->formatNum( $nitems ) ) .
92 "<p>" . wfMsg( "watcheditlist" ) . "</p>\n" );
93
94 $wgOut->addHTML( "<form action='" .
95 $specialTitle->escapeLocalUrl( "action=submit" ) .
96 "' method='post'>\n" .
97 "<ul>\n" );
98 $sql = "SELECT wl_namespace,wl_title FROM watchlist WHERE wl_user=$uid";
99 $res = wfQuery( $sql, DB_READ );
100 global $wgUser, $wgLang;
101 $sk = $wgUser->getSkin();
102 while( $s = wfFetchObject( $res ) ) {
103 $t = Title::makeTitle( $s->wl_namespace, $s->wl_title );
104 if( is_null( $t ) ) {
105 $wgOut->addHTML( '<!-- bad title "' . htmlspecialchars( $s->wl_title ) . '" in namespace ' . IntVal( $s->wl_namespace ) . " -->\n" );
106 } else {
107 $t = $t->getPrefixedText();
108 $wgOut->addHTML( "<li><input type='checkbox' name='id[]' value=\"" . htmlspecialchars($t) . "\" />" .
109 $sk->makeKnownLink( $t, $t ) .
110 "</li>\n" );
111 }
112 }
113 $wgOut->addHTML( "</ul>\n" .
114 "<input type='submit' name='remove' value='" .
115 wfMsg( "removechecked" ) . "' />\n" .
116 "</form>\n" );
117
118 return;
119 }
120
121 # If the watchlist is relatively short, it's simplest to zip
122 # down its entirety and then sort the results.
123
124 # If it's relatively long, it may be worth our while to zip
125 # through the time-sorted page list checking for watched items.
126
127 # Up estimate of watched items by 15% to compensate for talk pages...
128 if( $cutoff && ( $nitems*1.15 > $npages ) ) {
129 $x = "cur_timestamp";
130 $y = wfMsg( "watchmethod-recent" );
131 $z = "wl_namespace=cur_namespace&65534";
132 } else {
133 $x = "name_title_timestamp";
134 $y = wfMsg( "watchmethod-list" );
135 $z = "(wl_namespace=cur_namespace OR wl_namespace+1=cur_namespace)";
136 }
137
138
139 $wgOut->addHTML( "<i>" . wfMsg( "watchdetails",
140 $wgLang->formatNum( $nitems ), $wgLang->formatNum( $npages ), $y,
141 $specialTitle->escapeLocalUrl( "magic=yes" ) ) . "</i><br />\n" );
142
143 $use_index = wfUseIndexClause( $x, DB_READ );
144 $sql = "SELECT
145 cur_namespace,cur_title,cur_comment, cur_id,
146 cur_user,cur_user_text,cur_timestamp,cur_minor_edit,cur_is_new
147 FROM watchlist,cur $use_index
148 WHERE wl_user=$uid
149 AND $z
150 AND wl_title=cur_title
151 $docutoff
152 ORDER BY cur_timestamp DESC";
153
154
155 $res = wfQuery( $sql, DB_READ, $fname );
156
157 if($days >= 1)
158 $note = wfMsg( "rcnote", $wgLang->formatNum( $limit ), $wgLang->formatNum( $days ) );
159 elseif($days > 0)
160 $note = wfMsg( "wlnote", $wgLang->formatNum( $limit ), $wgLang->formatNum( round($days*24) ) );
161 else
162 $note = "";
163 $wgOut->addHTML( "\n<hr />\n{$note}\n<br />" );
164 $note = wlCutoffLinks( $days, $limit );
165 $wgOut->addHTML( "{$note}\n" );
166
167 if ( wfNumRows( $res ) == 0 ) {
168 $wgOut->addHTML( "<p><i>" . wfMsg( "watchnochange" ) . "</i></p>" );
169 return;
170 }
171
172 $sk = $wgUser->getSkin();
173 $s = $sk->beginRecentChangesList();
174 $counter = 1;
175 while ( $obj = wfFetchObject( $res ) ) {
176 # Make fake RC entry
177 $rc = RecentChange::newFromCurRow( $obj );
178 $rc->counter = $counter++;
179 $s .= $sk->recentChangesLine( $rc, true );
180 }
181 $s .= $sk->endRecentChangesList();
182
183 wfFreeResult( $res );
184 $wgOut->addHTML( $s );
185
186 if ( $wgUseWatchlistCache ) {
187 $wgMemc->set( $memckey, $s, $wgWLCacheTimeout);
188 }
189
190 }
191
192
193 function wlHoursLink( $h, $page ) {
194 global $wgUser, $wgLang;
195 $sk = $wgUser->getSkin();
196 $s = $sk->makeKnownLink(
197 $wgLang->specialPage( $page ),
198 $wgLang->formatNum( $h ),
199 "days=" . ($h / 24.0) );
200 return $s;
201 }
202
203
204 function wlDaysLink( $d, $page ) {
205 global $wgUser, $wgLang;
206 $sk = $wgUser->getSkin();
207 $s = $sk->makeKnownLink(
208 $wgLang->specialPage( $page ),
209 ($d ? $wgLang->formatNum( $d ) : wfMsg( "all" ) ), "days=$d" );
210 return $s;
211 }
212
213 function wlCutoffLinks( $days, $limit, $page = "Watchlist" )
214 {
215 $hours = array( 1, 2, 6, 12 );
216 $days = array( 1, 3, 7 );
217 $cl = "";
218 $i = 0;
219 foreach( $hours as $h ) {
220 $hours[$i++] = wlHoursLink( $h, $page );
221 }
222 $i = 0;
223 foreach( $days as $d ) {
224 $days[$i++] = wlDaysLink( $d, $page );
225 }
226 return wfMsg ("wlshowlast",
227 implode(" | ", $hours),
228 implode(" | ", $days),
229 wlDaysLink( 0, $page ) );
230 }
231
232 ?>